
Imports System.Collections
Imports System.Data.SqlClient
Imports System.Diagnostics
Imports _2bMatesSite.Common


Namespace _2bMatesSite.BO
	''' <summary>
	''' Summary description for SpecialPages.
	''' </summary>
	Public Class SpecialPages
		Protected list As ArrayList
		Protected hash As Hashtable

		Public Sub New()
			LoadSpecialPages()
		End Sub

		Public Shared Function GetSpecialPageByName(ByVal name As String) As SpecialPage
			Dim cmd As New SqlCommand("SELECT * FROM SpecialPages WHERE Name=@Name")
			cmd.Parameters.Add("@Name", name)

			Return GetSpecialPageByCmd(cmd)
		End Function

		Public Shared Function GetSpecialPageByID(ByVal id As Integer) As SpecialPage
			Dim cmd As New SqlCommand("SELECT * FROM SpecialPages WHERE ID=@ID")
			cmd.Parameters.Add("@ID", id)

			Return GetSpecialPageByCmd(cmd)
		End Function

		Public ReadOnly Property List() As ArrayList
			Get
				Return list
			End Get
		End Property

		Protected Sub LoadSpecialPages()
			list = New ArrayList()
			hash = New Hashtable()

			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()

			Try
				Dim cmd As New SqlCommand("SELECT * FROM SpecialPages ORDER BY Name ASC")
				cmd.Connection = conn

				Dim dr As SqlDataReader = cmd.ExecuteReader()

				Dim ordID As Integer = dr.GetOrdinal("ID")
				Dim ordName As Integer = dr.GetOrdinal("Name")

				While dr.Read()
					Dim spp As New SpecialPage(dr.GetInt32(ordID), dr.GetString(ordName))

					list.Add(spp)
					hash(spp.ID) = spp
				End While

				dr.Close()
			Finally
				conn.Close()
			End Try
		End Sub

		' Retrieve the special page using the given SQL command object
		' return null if no special page found
		Protected Shared Function GetSpecialPageByCmd(ByVal cmd As SqlCommand) As SpecialPage
			Dim spp As SpecialPage = Nothing

			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()

			Try
				cmd.Connection = conn
				Dim dr As SqlDataReader = cmd.ExecuteReader()

				If dr.Read() Then
					spp = New SpecialPage(dr)
					Debug.Assert(Not dr.Read())
				End If

				dr.Close()
			Finally
				conn.Close()
			End Try

			Return spp
		End Function
	End Class
End Namespace






